function setArrayNumber(array,item){
    let temp=[];
    for(let i=0;i<item.length;i++){
        temp.push(array[item[i]].Number);
    }
    return temp;
}
function isRepeat(arr){
    var hash = {};
    for(var i in arr) {
      if(hash[arr[i]]) {
        return true;
      }
      // 不存在该元素，则赋值为true，可以赋任意值，相应的修改if判断条件即可
      hash[arr[i]] = true;
    }
    return false;
}
function getNoID(array,item){
    let temp=[];
    for(let i=0;i<item.length;i++){
        temp.push(array[item[i]].NoID);
    }
    return temp;
}
function getBonus(array,item,multNum){
    let bonus = 1;
    for(let i=0;i<item.length;i++){
        bonus = bonus * array[item[i]].odds;
    }
    return (bonus * Number(multNum) * Football.PRICE).toFixed(2);
}
//----------------------------多串过关-------------------------------//
function removeRepeat(arr) {
    var obj = {};
    let array=[];
    for (var i = 0; i < arr.length; i++) {
        // 判断当前项是否遍历过，是则删除，否存入obj以作对照
        if (obj.hasOwnProperty(arr[i])) {
            continue;
        }
        array.push(arr[i]);
        obj[arr[i]] = i;
    }
    return array;
}
function CROSS(array) {
    var result = array.reduce((last, current) => {
        const temp = [];
        last.forEach(par1 => {
            current.forEach(par2 => {
                temp.push(par1 + "," + par2);
            });
        });
        return temp;
    });
    return result;
    //end
}
function setBetRecords(item, n, m) {
    let betRecords = [];
    let group = Football.mcom(n, m);
    //console.log(group);
    for (let j = 0; j < group.length; j++) {
        let tempGroup = [];
        for (let k = 0; k < group[j].length; k++) {
            tempGroup.push(item[group[j][k]]);
        }
        betRecords.push(tempGroup);
    }
    return betRecords;
}
function copyBetRecordsImte(betRecords, key) {
    let keys = key.split("_");
    let str = keys[0] + "_";
    let addArray = [];
    for (let i = 0; i < betRecords.length; i++) {
        let temp = [];
        for (let j = 0; j < betRecords[i].length; j++) {
            if (betRecords[i][j].indexOf(str) > -1) {
                temp = betRecords[i].concat();
                temp[j] = key;
                addArray.push(temp);
                break;
            }
        }
    }
    return addArray;
}
function copyRecursion(betRecords, CROSSlist, row, col) {
    if (row < CROSSlist.length) {
        if (col < CROSSlist[row].length) {
            let value = CROSSlist[row][col];
            let rs = copyBetRecordsImte(betRecords, value);
            betRecords = betRecords.concat(rs);
            col++;
            return copyRecursion(betRecords, CROSSlist, row, col);
        } else {
            row++;
            col = 1;
            return copyRecursion(betRecords, CROSSlist, row, col);
        }
    }
    return betRecords;
}
function getBetNumberCROSS(TYPE, betRecords, CROSSlist, row, col) {
    let item = [];
    if (row == 0 && col == 0) {
        for (let j = 0; j < CROSSlist.length; j++) {
            item.push(CROSSlist[j][0]);
        }
        row = 0;
        col++;
        let CROSSr = setBetRecords(item, CROSSlist.length, TYPE);
        betRecords = betRecords.concat(CROSSr);
        return getBetNumberCROSS(TYPE, betRecords, CROSSlist, row, col);
    } else {
        return copyRecursion(betRecords, CROSSlist, row, col);
    }
}
function getTypeGroup(lists) {
    //1.获得类型数组
    let objTypes = {};
    let types = [];
    for (let i = 0; i < lists.length; i++) {
        let noid = lists[i];
        let type = {};
        for (let j = 0; j < noid.length; j++) {
            let n = noid[j].split("_");
            let key = n[0] + "_" + n[1];
            if (type.hasOwnProperty(key)) {
                type[key].push(noid[j]);
            } else {
                type[key] = [noid[j]];
            }
        }
        let array = [];
        for (let i in type) {
            array.push(i);
            objTypes[i] = type[i];
        }
        types.push(array);
    }
    /*类型笛卡尔积集合*/
    var result = CROSS(types);
    return {
        objTypes:objTypes,
        result:result
    };
}
function getTypeGroupbetNumberCROSS(lists) {
    //1.获得类型数组
    let {objTypes,result} = getTypeGroup(lists);
    //2.投注代入类型
    let betNumberCROSS = [];
    for (let i = 0; i < result.length; i++) {
        let item = result[i].split(',');
        let temp = [];
        for (let j = 0; j < item.length; j++) {
            temp.push(objTypes[item[j]]);
        }
        betNumberCROSS.push(temp);
    }
    return betNumberCROSS;
}
//------------------------------------------------------------//
var duoguanType={
    "1":{},
    "2":{},
    "3":{
        "3串3":{"2串1":3,"3串1":0,"4串1":0,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "3串4":{"2串1":3,"3串1":1,"4串1":0,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
    },
    "4":{
        "4串4":{"2串1":0,"3串1":4,"4串1":0,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "4串5":{"2串1":0,"3串1":4,"4串1":1,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "4串6":{"2串1":6,"3串1":0,"4串1":0,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "4串11":{"2串1":6,"3串1":4,"4串1":1,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
    },
    "5":{
        "5串5":{"2串1":0,"3串1":0,"4串1":5,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "5串6":{"2串1":0,"3串1":0,"4串1":5,"5串1":1,"6串1":0,"7串1":0,"8串1":0},
        "5串10":{"2串1":10,"3串1":0,"4串1":0,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "5串16":{"2串1":0,"3串1":10,"4串1":5,"5串1":1,"6串1":0,"7串1":0,"8串1":0},
        "5串20":{"2串1":10,"3串1":10,"4串1":0,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "5串26":{"2串1":10,"3串1":10,"4串1":5,"5串1":1,"6串1":0,"7串1":0,"8串1":0},
    },
    "6":{
        "6串6":{"2串1":0,"3串1":0,"4串1":0,"5串1":6,"6串1":0,"7串1":0,"8串1":0},
        "6串7":{"2串1":0,"3串1":0,"4串1":0,"5串1":6,"6串1":1,"7串1":0,"8串1":0},
        "6串15":{"2串1":15,"3串1":0,"4串1":0,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "6串20":{"2串1":0,"3串1":20,"4串1":0,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "6串22":{"2串1":0,"3串1":0,"4串1":15,"5串1":6,"6串1":1,"7串1":0,"8串1":0},
        "6串35":{"2串1":15,"3串1":20,"4串1":0,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "6串42":{"2串1":0,"3串1":20,"4串1":15,"5串1":6,"6串1":1,"7串1":0,"8串1":0},
        "6串50":{"2串1":15,"3串1":20,"4串1":15,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "6串57":{"2串1":15,"3串1":20,"4串1":15,"5串1":6,"6串1":1,"7串1":0,"8串1":0},
    },
    "7":{
        "7串7":{"2串1":0,"3串1":0,"4串1":0,"5串1":0,"6串1":7,"7串1":0,"8串1":0},
        "7串8":{"2串1":0,"3串1":0,"4串1":0,"5串1":0,"6串1":7,"7串1":1,"8串1":0},
        "7串21":{"2串1":0,"3串1":0,"4串1":0,"5串1":21,"6串1":0,"7串1":0,"8串1":0},
        "7串35":{"2串1":0,"3串1":0,"4串1":35,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "7串120":{"2串1":21,"3串1":35,"4串1":35,"5串1":21,"6串1":7,"7串1":1,"8串1":0},
    },
    "8":{
        "8串8":{"2串1":0,"3串1":0,"4串1":0,"5串1":0,"6串1":0,"7串1":8,"8串1":0},
        "8串9":{"2串1":0,"3串1":0,"4串1":0,"5串1":0,"6串1":0,"7串1":8,"8串1":1},
        "8串28":{"2串1":0,"3串1":0,"4串1":0,"5串1":0,"6串1":28,"7串1":0,"8串1":0},
        "8串56":{"2串1":0,"3串1":0,"4串1":0,"5串1":56,"6串1":0,"7串1":0,"8串1":0},
        "8串70":{"2串1":0,"3串1":0,"4串1":70,"5串1":0,"6串1":0,"7串1":0,"8串1":0},
        "8串247":{"2串1":28,"3串1":56,"4串1":70,"5串1":56,"6串1":28,"7串1":8,"8串1":1},
    },
}
var Football = {
    gameListsData:{}
}
Football.setGameListsData=(array)=>{
    let obj = {};
    for (let i =0;i<array.length;i++){
        obj[array[i].number]=array[i];
    }
    Football.gameListsData=obj;
}
Football.PRICE=2;
Football.TYPE={
    '单关':1,
    '2串1':2,
    '3串1':3,
    '4串1':4,
    '5串1':5,
    '6串1':6,
    '7串1':7,
    '8串1':8
}
Football.Danguan={
    '1':'单关',
    '2':'2串1',
    '3':'3串1',
    '4':'4串1',
    '5':'5串1',
    '6':'6串1',
    '7':'7串1',
    '8':'8串1',
}
Football.Duoguan={}
Football.setDuoguan=()=>{
    let duoguan={};
    for(let k in duoguanType){
        if(Object.keys(duoguanType[k])==0){
            continue;
        }
        duoguan = Object.assign(duoguan,duoguanType[k]);
    }
    Football.Duoguan=duoguan;
    console.log(Football.Duoguan);
}
Football.setDuoguanLists=(count)=>{
    let lists=[];
    for(let k in duoguanType){
        if(Number(k)>count){
            break;
        }
        if(Object.keys(duoguanType[k])==0){
            continue;
        }
        let item =duoguanType[k];
        for(let j in item){
            lists.push(j);
        }
    }
    return lists; 
}
Football.mcom=(n,m)=>{
    var rt=[];
    for (var i=0;i<Math.pow(2,n);i++){
        var a=0;
        var tmp=[];
        for (var j=0;j<n;j++){
            if(i>>j & 1){
                a++;
                tmp.push(j);
            }
        }
        if(a==m){
            rt.push(tmp);
        }
    }
    return rt;
}
/**
 * @param array
 * @param name
 * @returns {{betNumber: *, betRecords: *, name: *}}
 * 自由过关
 */
Football.DanchuanComb=(betNumber,name,multNum)=>{
    let arrayBetNumber=[];
    for(let i in betNumber){
        arrayBetNumber.push(betNumber[i]);
    }
    let betRecords=[];
    for (let i =0;i<name.length;i++){
        betRecords = betRecords.concat(Football.combC(arrayBetNumber,name[i],multNum));
    }
    return {
        name:name.join(','),
        betNumber:arrayBetNumber,
        betRecords:betRecords
    }
}
/**
 * 自由过关
 */
Football.combC=(array,name,multNum)=>{
    //拿出胆的NoID
    let danNoID=[];
    for(let i=0;i<array.length;i++){
        if(array[i].dan){
            danNoID.push(array[i].NoID);
        }
    }
    //end
    let betRecords=[];
    let group=Football.mcom(array.length,Football.TYPE[name]);
    for(let i =0;i<group.length;i++){
        //验证Number是否有重复
        if(isRepeat(setArrayNumber(array,group[i]))){
            continue;
        }
        let buyNumber=getNoID(array,group[i]);
        //如果有胆，没有胆的记录直接过
        if(danNoID.length>0){
            let temp = buyNumber.filter(function(v){ return danNoID.indexOf(v) > -1 });//交集
            if(temp.length==0){
                continue;
            }
        }
        //end
        let temp = {
            "name": name, //过关的名称
            "buyNumber":buyNumber.join('|'), // betNumber中的NoID 用|分割
            "multNum":parseInt(multNum), //倍数
            "numNo": group[i].length,  //排列组合的长度
            "bonus":getBonus(array,group[i],multNum)
        }
        betRecords.push(temp);
    }
    return betRecords;
}
/**
 * 多串过关
 * 1.类型笛卡尔积分组
 * 2.查找代入替换
 * @param lists
 * @param betNumber
 * @param name
 * @param multNum
 * @constructor
 */
Football.DuoguanComb = (lists,betNumber,name,multNum) => {

    let numberLlists=[];
    for (let i =0;i<lists.length;i++){
        numberLlists.push(lists[i].NoIDs);
    }
    let betRecords=Football.combA(numberLlists,name);
    console.log(betRecords);
}
Football.combA=(lists,name)=>{
    let object = Football.Duoguan[name[0]];
    let names = [];
    for (let k in object) {
        if (object[k] > 0) {
            names.push(k);
        }
    }
    //投注代入类型
    let betNumberCROSS = getTypeGroupbetNumberCROSS(lists);
    var betRecords=[];
    for(let n=0;n<names.length;n++){
        let END_TYPE = Football.TYPE[names[n]];
        for(let i = lists.length-1;i>END_TYPE;i--){
            let arrayBetNumber=[];
            for(let j =0;j<betNumberCROSS.length;j++){
                let array = setBetRecords(betNumberCROSS[j],betNumberCROSS[j].length,i);
                arrayBetNumber = arrayBetNumber.concat(array);
            }
            betNumberCROSS=removeRepeat(arrayBetNumber);
        }
        let data = Football.getTypeBetNumberCROSS(END_TYPE, betNumberCROSS);
        betRecords=betRecords.concat(data);
    }
    return betRecords;
}
Football.getTypeBetNumberCROSS = (TYPE, betNumberCROSS) => {
    let betRecords = [];
    for (let i = 0; i < betNumberCROSS.length; i++) {
        let data = getBetNumberCROSS(TYPE, [], betNumberCROSS[i], 0, 0);
        betRecords = betRecords.concat(data);
        //console.log(data);
    }
    return betRecords;
}

Football.setDuoguan();
module.exports = Football;